/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 
 */
public class Line {
    float[] m_e1;
    float[] m_e2;
    public Line(float[] e1, float[] e2) {
        m_e1 = new float[e1.length];
        m_e2 = new float[e1.length];
        for (int i = 0;i < e1.length;i++) {
            m_e1[i] = e1[i];
            m_e2[i] = e2[i];
        }
    }
    public Line(int[] e1, int[] e2) {
            m_e1 = new float[e1.length];
        m_e2 = new float[e1.length];
        for (int i = 0;i < e1.length;i++) {
            m_e1[i] = e1[i];
            m_e2[i] = e2[i];
        }
};
    float [] deltas() {
        float d[] = new float[m_e1.length];
        for (int i = 0;i < m_e1.length;i++) {
            d[i] = m_e2[i]-m_e1[i];
        }
        return d;
    }
    float[] intersects(Line b) {
        // a.x0+a.dx*a.t = x  Line 1
        // a.y0+a.dy*a.t = y  Line 1
        // b.x0+b.dx*b.t = x  Line 2
        // b.y0+b.dy*b.t = y  Line 2
        // a.x0+a.dx*a.t-b.x0-b.dx*b.t = 0  when intersect is true
        // a.y0+a.dy*a.t-b.y0-b.dy*b.t = 0  when intersect is true
        // a.t=(-a.x0+b.x0+b.dx*b.t)/a.dx
        // a.t=(-a.y0+b.y0+b.dy*b.t)/a.dy
        // (-a.x0+b.x0+b.dx*b.t)/a.dx = (-a.y0+b.y0+b.dy*b.t)/a.dy
        // (-a.x0+b.x0+b.dx*b.t)*a.dy = (-a.y0+b.y0+b.dy*b.t)*a.dx
        // (-a.x0 * a.dy)+(b.x0 * a.dy)+ b.dx*b.t*a.dy = (-a.y0*a.dx)+ (b.y0*a.dx)+ b.dy*b.t*a.dx
        // (b.dx*b.t*a.dy) - (b.dy*b.t*a.dx) = (-a.y0*a.dx)+ b.y0*a.dx - (-a.x0*a.dy)-(b.x0*a.dy)
        // b.t(b.dx*a.dy-b.dy*a.dx) = a.x0*a.dy - a.y0*a.dx + b.y0*a.dx - b.x0*a.dy
        // b.t = (a.x0*a.dy - a.y0*a.dx + b.y0*a.dx - b.x0*a.dy)/ (b.dx*a.dy-b.dy*a.dx)
        ////////////////////////////////////////////////////////////////////////////////
        // 0=[(-a.x0+b.x0+b.dx*b.t)/a.dx]-[(-a.y0+b.y0+b.dy*b.t)/a.dy]
        // -(-a.x0/a.dx)-(b.x0/a.dx)+(-a.y0/a.dy)+(b.y0/a.dy)=(b.dx*b.t/a.dx)-(b.dy*b.t/a.dy)
        // temp =  -(-a.x0/a.dx)-(b.x0/a.dx)+(-a.y0/a.dy)+(b.y0/a.dy)
        // temp = (b.dx*b.t/a.dx)-(b.dy*b.t/a.dy)
        // temp*a.dx*a.dy=(b.dx*b.t*a.dy)-(b.dy*b.t*a.dx)
        // temp*a.dx*a.dy=b.t(b.dx*a.dy-b.dy*a.dx)
        //(temp*a.dx*a.dy)/(b.dx*a.dy-b.dy*a.dx)=b.t  
        
        
        // b.t = (a.x0*a.dy - a.y0*a.dx + b.y0*a.dx - b.x0*a.dy)/ (b.dx*a.dy-b.dy*a.dx)
        // b.t = [(a.dy*(a.x0-b.x0) - a.dx*(a.y0-b.y0)]/ (b.dx*a.dy-b.dy*a.dx)
        // float x0diff = m_e1[0]-b.m_e1[0];
        // float y0diff = m_e1[1]-b.m_e1[1];
        float[] adxy = deltas();   // a.dx and a.dy
        float[] bdxy = b.deltas(); // b.dx and b.dy
        float left = (adxy[1]*(m_e1[0]-b.m_e1[0]))-(adxy[0]*(m_e1[1]-b.m_e1[1]));
        float right = bdxy[0]*adxy[1]-bdxy[1]*adxy[0];

        // float right = -(x0diff-y0diff);
        // float left = adxy[0]-bdxy[0]-adxy[1]+bdxy[1];
        float bt = left/right;
 
        // b.x0+b.dx*b.t = x  Line 2
        // b.y0+b.dy*b.t = y  Line 2
        // a.x0+a.dx*a.t-b.x0-b.dx*b.t = 0
        float ix = b.m_e1[0]+bdxy[0]*bt;
        float iy = b.m_e1[1]+bdxy[1]*bt;
       
        float[] res = new float[2];
        res[0] = ix;
        res[1] = iy;
        return res;
        
        
    }
    
    
}
